package controller;

import dao.CommodityDao;
import dao.DbdetailsDao;
import dao.InventoryDao;
import dao.TransferDao;
import entity.*;

import com.fasterxml.jackson.databind.util.Converter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.DbdetailsService;
import service.TransferService;
import util.random;
import util.random1;
import util.random2;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/KCGL")
public class KCGL {
    @Resource(name = "transferService")
    private TransferService transfeServicer;

    @Resource(name="dbdetailsService")
    private DbdetailsService dbdetailsService;

    @Resource(name="dbdetailsDao")
    private DbdetailsDao dbdetailsDao;

    @Resource(name="transferDao")
    private TransferDao transferDao;

    @Resource(name = "inventoryDao")
    private InventoryDao inventoryDao;

    @Resource(name = "commodityDao")
    private CommodityDao commodityDao;

    @RequestMapping("/KCGL")
    public String KCGL(){
        return "/WEB-INF/jsp/KuCunGuanLi/KCGL";
    }
    @RequestMapping("/KCDB")
    public String KCDB(Model model){
        List<Transfer> list = transfeServicer.query();

        random1 r = new random1();
        String fan = r.random(list);
        model.addAttribute("order", fan);


        return "/WEB-INF/jsp/KuCunGuanLi/KCDB";
    }
    @RequestMapping("/KCPD")
    public String KCPD(){
        return "/WEB-INF/jsp/KuCunGuanLi/KCPD";
    }
    @RequestMapping("/querykcdb")
    @ResponseBody
    public List<Transfer> query(Model model){
        List<Transfer> list = transfeServicer.query();
        return list;
    }
    @RequestMapping("/querydatekcdb")
    @ResponseBody
    public List<Transfer> querydate(String start,String end){
        List<Transfer> list = transfeServicer.querydate(start,end);
        return list;
    }
    @RequestMapping("/queryorder")
    @ResponseBody
    public List<Dbdetails> queryorder(String ordernumber){
        return dbdetailsService.ordernumber(ordernumber);
    }


    @RequestMapping("/add")
    @ResponseBody
    public List<Dbdetails> add(Dbdetails db){
            Dbdetails db2 = dbdetailsService.queryone(db);
            if (db2!=null){
                db.setNumber(db2.getNumber()+db.getNumber());
                int a = dbdetailsService.update(db);
            }else if(db2==null){
                db.setOrdernumber("123456");
                int b = dbdetailsService.add(db);
            }
        return dbdetailsDao.query(db.getState());
    }


    @RequestMapping("/update")
    @ResponseBody
    public List<Dbdetails> update(String bianhao,String state,String number){
        String aa = bianhao.substring(1,bianhao.toCharArray().length-1);
        String bb = state.substring(1,state.toCharArray().length-1);
        String cc = number.substring(1,number.toCharArray().length-1);
        String[] a = aa.split(",");
        String[] b = bb.split(",");
        String[] c = cc.split(",");
        for(int i = 0;i<a.length;i++){

            String bian = a[i].substring(1,a[i].toCharArray().length-1);
            Dbdetails one = new Dbdetails();
            one.setState(2);
            one.setBianhao(bian);
            Dbdetails db = dbdetailsService.queryone(one);
            if(db==null){
                int gai = dbdetailsDao.updatestate();
                break;
            }
            if(db!=null){
                db.setNumber(db.getNumber()+Integer.parseInt(c[i]));
                dbdetailsService.update(db);
                dbdetailsDao.deleteone(bian);

            }
        }
        return dbdetailsDao.query(2);
    }
    @RequestMapping("/query2")
    @ResponseBody
    public List<Dbdetails> query2(){
        return dbdetailsDao.query(2);
    }
    @RequestMapping("/queding")
    @ResponseBody
    public List<Dbdetails> queding(Transfer tran,String chanpinname,String shuliang,int diaochu1,int diaoru1){
        System.out.println(chanpinname+"---"+shuliang);
        String shu = shuliang.substring(1,shuliang.toCharArray().length-1);
        String name = chanpinname.substring(1,chanpinname.toCharArray().length-1);
        String[] shu1 = shu.split(",");
        String[] name1 = name.split(",");
        for(int i = 0;i<shu1.length;i++){
            String ming = name1[i].substring(1,name1[i].toCharArray().length-1);
            //查询被调入仓库有没有此商品
            Commodity queryone = commodityDao.queryone(ming,diaoru1);
            int tianjia = 0;
            int diaoru = 0;
            int diaochu = 0;
            if(queryone==null || queryone.equals("")){
                Commodity querytwo = commodityDao.queryone(ming,diaochu1);
                //把调出仓库商品减去相应的数量
                diaochu = commodityDao.dbupdate(ming,diaochu1,(int)querytwo.getStock()-Integer.parseInt(shu1[i]));
                //要添加把数量改成要添加的数量
                querytwo.setStock(Integer.parseInt(shu1[i]));
                querytwo.setWarehouseid(diaoru1);
                //执行添加
                tianjia = commodityDao.dbadd(querytwo);

            }else{
                Commodity querytwo = commodityDao.queryone(ming,diaochu1);
                diaoru = commodityDao.dbupdate(ming,diaoru1,(int)queryone.getStock()+Integer.parseInt(shu1[i]));
                diaochu = commodityDao.dbupdate(ming,diaochu1,(int)querytwo.getStock()-Integer.parseInt(shu1[i]));
            }
        }
        int a = dbdetailsDao.updateorder(tran.getOrdernumber());
        int b = transferDao.insert(tran);
        return dbdetailsDao.query(2);
    }
}
